package com.baiytfp.hf.HFDataExchange.action.controller.view;

import com.baiytfp.hf.baiytfp.framework.base.BaseController;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import com.baiytfp.hf.baiytfp.hfutils.HFRequest;
import com.baiytfp.hf.HFDataExchange.action.service.DataDictService;
import com.baiytfp.hf.HFDataExchange.action.service.MenuService;
import com.baiytfp.hf.HFDataExchange.action.service.TreeService;
import com.baiytfp.hf.HFDataExchange.common.base.anotation.Auth;
import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.state.TreeType;
import com.baiytfp.hf.HFDataExchange.common.model.table.Data_dict;
import com.baiytfp.hf.HFDataExchange.common.model.table.Menu;
import com.baiytfp.hf.HFDataExchange.common.model.table.Tree;
import com.baiytfp.hf.HFDataExchange.common.model.vo.BaseUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
@RequestMapping("/view/datadict")
public class DataDictVController extends BaseController {



	@Autowired
	public DataDictService datadictService;

	@Autowired
	public TreeService treeService;

	@RequestMapping("/manage")
	@Auth(checkMenu=true)
	public ModelAndView manage(HttpServletRequest request,HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();

		//可以跨父结点拖动页面
//		mav.setViewName("/menu/manage2");
		return mav;
	}

	@RequestMapping("/tnodeadd")
	@Auth(checkMenu=true)
	public ModelAndView tnodeadd(HttpServletRequest request,HttpServletResponse response) throws Exception {

		int treeType = TreeType.DATA_DICT.getKey();

		String parentTreeCode = HFRequest.getIstance().getString(request,"parent_tree_code", "-1");

		ModelAndView mav = new ModelAndView();

		if(!"-1".equals(parentTreeCode)){
			Tree parentTree = treeService.getNodeInfo(parentTreeCode, treeType + "");
			mav.addObject("pNode", parentTree);
		}else{
			Tree parentTree = new Tree();
			parentTree.setNode_name("无");
			mav.addObject("pNode", parentTree);
		}
		mav.addObject("nodeDeep", HFRequest.getIstance().getInt(request,"node_deep", 0));
		mav.addObject("parentTreeCode", parentTreeCode);
		mav.addObject("treeType", treeType);

		return mav;
	}

	@RequestMapping("/tnodeedit")
	@Auth(checkMenu=true)
	public ModelAndView tnodeedit(HttpServletRequest request,HttpServletResponse response) throws Exception {

		int treeType = TreeType.DATA_DICT.getKey();

		String treeCode = HFRequest.getIstance().getString(request,"TREE_CODE", "");

		//读取树结点
		Tree treeVO = treeService.getNodeInfo(treeCode, treeType + "");

		Data_dict datadictVO = HFDBHelper.read(TableName.DATA_DICT, " t where exists (select id from " + TableName.TREE + " where id=t.tree_id and tree_code = ? ) ", Data_dict.class, treeCode);


		ModelAndView mav = new ModelAndView();


		if(!"-1".equals(treeVO.getParent_tree_code())){	//读取父结点信息
			Tree parentTree = treeService.getNodeInfo(treeVO.getParent_tree_code(), treeType + "");
			mav.addObject("pNode", parentTree);
		}else{
			Tree parentTree = new Tree();
			parentTree.setNode_name("无");
			mav.addObject("pNode", parentTree);
		}

		mav.addObject("tNode", treeVO);
		mav.addObject("nodeAttr", datadictVO);
		mav.addObject("treeType", treeType);


		return mav;
	}



}
